package com.bkitmobile.kesu.appengine.web.client.SupportPopup;

import java.util.HashMap;

import com.bkitmobile.kesu.appengine.web.client.GreetingService;
import com.bkitmobile.kesu.appengine.web.client.GreetingServiceAsync;
import com.bkitmobile.kesu.appengine.web.client.entity.UserInfo;
import com.bkitmobile.kesu.appengine.web.client.utility.LocationInfoPopupConstant;
import com.bkitmobile.kesu.appengine.web.client.utility.RegisterPopupConstant;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class FeePayingPopup extends DialogBox {
	private HashMap<String, Widget> InputParam = 
		new HashMap<String, Widget> ();
	private final FormPanel form = new FormPanel();
	private double Longtitude;
	private double Latitude;
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);
	

	private HorizontalPanel createTextPanel (String title)
	{
		HorizontalPanel create = new HorizontalPanel();
		Label name = new Label();
		name.setText(title);
		name.setPixelSize(250, 20);
		create.add(name);
		
		TextBox input = new TextBox();
		input.setPixelSize(300, 20);
		create.add(input);
		InputParam.put(title, input);
		
		return create;
	}
	
	private HorizontalPanel createLabelPanel (String title, String info)
	{
		HorizontalPanel create = new HorizontalPanel();
		Label name = new Label();
		name.setText(title);
		name.setPixelSize(150, 20);
		create.add(name);
		
		Label input = new Label();
		input.setPixelSize(300, 20);
		input.setText(info);
		create.add(input);
		
		return create;
	}
	
	private void inputInfo (final UserInfo info)
	{
		//create the form panel, set its action and method
		form.setHeight("300px");
		form.setMethod(FormPanel.METHOD_GET);
		//set the main widget for the panel to a vertical panel
		VerticalPanel panel = new VerticalPanel();
		panel.setSpacing(4);
		panel.add(createLabelPanel("T\u00ean t\u00e0i kho\u1ea3n", info.getUserName()));
		panel.add(createLabelPanel("H\u1ecd", info.getfName()));
		panel.add(createLabelPanel("T\u00ean", info.getlName()));
		panel.add(createLabelPanel("S\u1ed1 \u0111\u1ecba \u0111i\u1ec3m c\u00f3 " +
				"tr\u1ea3 ph\u00ed c\u00f2n l\u1ea1i", Integer.toString(info.getPlaceLeft())));
		
		HorizontalPanel num_Of_Day = new HorizontalPanel();
		panel.add(num_Of_Day);
		Label num_of_day_title = new Label("S\u1ed1 ng\u00e0y mu\u1ed1n mua");
		num_of_day_title.setPixelSize(250, 20);
		num_Of_Day.add(num_of_day_title);
		
		final ListBox days = new ListBox();
		for (int i = 0;i<100;i++)
			days.addItem(Integer.toString(i));
		num_Of_Day.add(days);
		
		days.addChangeListener(new ChangeListener() {
			@Override
			public void onChange(Widget sender) {
				// TODO Auto-generated method stub
				Window.alert("B\u1ea1n s\u1ebd ph\u1ea3i tr\u1ea3 t\u1ed5ng c\u1ed9ng "+ 
						Integer.toString(days.getSelectedIndex()*5000) + " VND");
			}
		});
		
		
		Button add_btn = new Button();
		add_btn.setText("Thay \u0111\u1ed5i");
		Button cancel_btn = new Button();
		cancel_btn.setText("Tho\u00e1t");
		
		/////// Xu ly su kien add thanh cong
		cancel_btn.addClickListener(new ClickListener() {
			
			@Override
			public void onClick(Widget sender) {
				// TODO Auto-generated method stub
				hide();
			}
		});
		
		add_btn.addClickListener(new ClickListener() {
			
			@Override
			public void onClick(Widget sender) {
				// TODO Auto-generated method stub
				final int dayLeft = info.getPlaceLeft();
				info.setPlaceLeft(dayLeft + days.getSelectedIndex());
				
				greetingService.doUpdate(info, new AsyncCallback<String>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						Window.alert(Integer.toString(dayLeft + days.getSelectedIndex()));
					}

					@Override
					public void onSuccess(String result) {
						// TODO Auto-generated method stub
						Window.Location.reload();
					}
				});
			}
		});
		
		
		
		//////// Add vao panel
		HorizontalPanel button = new HorizontalPanel();
		button.add(add_btn);
		button.add(cancel_btn);
		button.setSpacing(5);
		panel.add(button);
		form.setWidget(panel);
		////// khong dung add vi glass panel se che mat popup
		this.setWidget(form);
		this.setPopupPosition(425, 146);
	}
	
	
	public FeePayingPopup()
	{
		this.setText("Mua \u0111\u1ecba \u0111i\u1ec3m c\u00f3 tr\u1ea3 ph\u00ed");
		// Enable animation.
		setAnimationEnabled(true);

		  
		// Enable glass background.
		setGlassEnabled(true);

		greetingService.checkLogin("", new AsyncCallback<String>() {
			
			@Override
			public void onSuccess(String result_fr) {
				// TODO Auto-generated method stub
				if (!result_fr.equals(""))
				{
					greetingService.getUsrInfo(result_fr, new AsyncCallback<UserInfo>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onSuccess(UserInfo result) {
							// TODO Auto-generated method stub
							if (result == null)
							{
								Window.alert("\u0110\u00e3 c\u00f3 l\u1ed7i x\u1ea3y ra, " +
										"xin b\u1ea1n vui l\u00f2ng tho\u00e1t ra v\u00e0 \u0111\u0103ng nh\u1eadp l\u1ea1i");
								hide();
							}
							else
								inputInfo(result);
						}
					});
				}
				else
				{
					Window.alert("\u0110\u00e3 c\u00f3 l\u1ed7i x\u1ea3y " +
							"ra, xin b\u1ea1n vui l\u00f2ng tho\u00e1t ra v\u00e0 \u0111\u0103ng nh\u1eadp l\u1ea1i");
					hide();
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});

		////// khong dung add vi glass panel se che mat popup
		this.setWidget(form);
		this.setPopupPosition(425, 146);
	}
	
}
